"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 更新数据的过程中恢复不同数据库
Create At   : 2020/11/23
@zou_jialiang0505328126
Description :
    1.创建数据库tpcc，在数据库tpcc中创建表
    2.更新数据
    3.更新数据过程中恢复postgres数据库
    4.检查数据一致性
    5.执行TPCC
    6.检查数据一致性
Expect      :
    1.创建表成功
    2.更新数据成功
    3.恢复成功
    4.数据一致
    5.执行tpcc成功
    6.数据一致
History     :
    modified: 2020/03/01  modified by @peilinqian,修改创建数据库SQL；以防已存在库导致断言失败
    modified: 2020/12/11  modified by @wan005,修改恢复数据库为新创建数据库，防止恢复失败导致后续用例失败
    modified: 2021/01/08  modified by @wan005,物化视图不支持compression等参数，修改断言适配1.1.0版本
    modified: 2021/08/16  modified by @wan005,增加同步等待时长
"""

import os
import unittest
import time
import sys
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    commonshsta2 = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Tools_Restore_Case008.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbStandby2DbUser = Node(node='Standby2DbUser')
        self.dbPrimaryRootNode = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.restore_file = os.path.join(macro.DB_BACKUP_PATH, 'dump_backup', 'tools_restore_case001.tar')
        self.sql_path = os.path.join(macro.DB_BACKUP_PATH, 'testscript')
        self.tblname = 'restore_case008'
        self.values = '9000000'
        self.updatenum = '8000000'

        self.log.info('-------------------创建备份路径-----------------------------------')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)}; mkdir {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('-----------------------生成预设数据-----------------------')
        text = '----step1：生成预设数据 expect:成功----'
        result = self.common.scp_file(self.dbPrimaryDbUser, 'restore_data.sql', self.sql_path)
        self.log.info(result)
        sql_file_cmd = f'''
                                source {macro.DB_ENV_PATH};
                                gsql -d {self.dbPrimaryDbUser.db_name} -p {self.dbPrimaryDbUser.db_port}  -f {os.path.join(
            self.sql_path, 'restore_data.sql')}
                                '''
        self.log.info(sql_file_cmd)
        sql_bx_msg = self.dbPrimaryDbUser.sh(sql_file_cmd).result()
        self.log.info(sql_bx_msg)
        self.assertNotIn('ERROR', sql_bx_msg, '执行失败:' + text)

        self.log.info('--------------------导出数据----------------------------')
        text = '----step2：导出数据 expect:成功----'
        result = self.commonshpri.dumpFile(filename=self.restore_file, get_detail=False)
        self.log.info(result)
        self.assertTrue(result, '执行失败:' + text)

        self.dbname = 'dbsys_restore_db'
        self.log.info("---------------------创建数据库--------------------")
        text = '----step3：创建数据库 expect:成功----'
        create_db_sql = f'drop database if exists {self.dbname};' \
            f'create database {self.dbname};'
        result = self.commonshpri.executDbSql(create_db_sql)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS_MSG,
                      result, '执行失败:' + text)

    def test_basebackup(self):
        self.log.info('----------------------创建表，插入数据,创建索引------------------------')
        text = '----step4：创建表，插入数据,创建索引 expect:成功----'
        result = self.commonshpri.executDbSql(
            f'create table {self.tblname}(i int); insert into {self.tblname} (select * from generate_series(1,{self.values}));')
        self.log.info(result)
        self.assertIn('INSERT', result, '执行失败:' + text)

        self.log.info('--------------------获取还原前数据库已有表------------------------')
        self.table_list = self.commonshpri.executDbSql(r'\d')
        self.log.info(self.table_list)

        self.log.info('-------------------启动线程创建表,更新数据(tpcc)-----------------------------------')
        insert_thread = ComThread(self.commonshpri.executDbSql,
                                  args=(f'update {self.tblname} set i=0 where i<={self.updatenum};',))
        insert_thread.setDaemon(True)
        insert_thread.start()

        self.log.info('-------------------恢复数据-----------------------------------')
        text = '----step5：恢复数据 expect:成功----'
        result = self.commonshpri.restoreFile(self.restore_file, get_detail=True, dbname=self.dbname)
        self.log.info(result)
        self.assertIn(self.Constant.RESTORE_SUCCESS_MSG, result,
                      '执行失败:' + text)

        self.log.info('----------------------检查插入结果-------------------------')
        text = '----step6：启动线程创建表,更新数据 expect:成功----'
        insert_thread.join(60 * 60)
        result = insert_thread.get_result()
        self.log.info(result)
        self.assertIn(f'UPDATE {self.updatenum}', result, '执行失败:' + text)

        self.log.info('----------------------------对比恢复前后数据是否一致-------------------------')
        time.sleep(5)
        text = '----step7：对比恢复前后数据是否一致 expect:成功----'
        self.table_list_after = self.commonshpri.executDbSql(r'\d')
        self.log.info(self.table_list_after)
        self.assertIn(self.table_list, self.table_list_after, '执行失败:' + text)

        self.log.info('-------------------检查主备是否同步-------------------')
        text = '----step8：检查主备是否同步 expect:成功----'
        result = self.commonshsta.check_data_consistency()
        if not result:
            result = self.commonshsta.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)
        result = self.commonshsta2.check_data_consistency()
        if not result:
            result = self.commonshsta2.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)

        self.log.info('--------------------检查数据一致性---------------------')
        text = '----step9：检查数据一致性 expect:成功----'
        nodes_tuple = (self.dbPrimaryDbUser, self.dbStandby1DbUser, self.dbStandby2DbUser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag, '执行失败:' + text)

        table_dict = self.common.format_sql_result(self.commonshpri.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag, '执行失败:' + text)

        self.log.info('------------------启动TPCC----------------------------')
        text = '----step10：启动TPCC expect:成功----'
        result = self.common.startTPCC(self.dbPrimaryRootNode, macro.TPCC_PATH)
        self.log.info(result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result, '执行失败:' + text)

        self.log.info('-------------------检查主备是否同步-------------------')
        text = '----step11：检查主备是否同步 expect:成功----'
        result = self.commonshsta.check_data_consistency()
        if not result:
            result = self.commonshsta.check_data_consistency()
        self.assertTrue(result)
        result = self.commonshsta2.check_data_consistency()
        if not result:
            result = self.commonshsta2.check_data_consistency()
        self.assertTrue(result,  '执行失败:' + text)

        self.log.info('--------------------检查数据一致性---------------------')
        text = '----step12：检查数据一致性 expect:成功----'
        nodes_tuple = (self.dbPrimaryDbUser, self.dbStandby1DbUser, self.dbStandby2DbUser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag,  '执行失败:' + text)

        table_dict = self.common.format_sql_result(self.commonshpri.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag,  '执行失败:' + text)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------------------删除备份文件------------------')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('--------------------------删除预设数据---------------------')
        result = self.common.scp_file(self.dbPrimaryDbUser, 'clear_restore_data.sql', self.sql_path)
        self.log.info(result)
        sql_file_cmd = f'''
                                       source {macro.DB_ENV_PATH};
                                       gsql -d {self.dbPrimaryDbUser.db_name} -p {self.dbPrimaryDbUser.db_port}  -f {os.path.join(
            self.sql_path, 'clear_restore_data.sql')}
        '''
        self.log.info(sql_file_cmd)
        sql_bx_msg = self.dbPrimaryDbUser.sh(sql_file_cmd).result()
        self.log.info(sql_bx_msg)

        self.log.info('----------------------删除执行脚本路径----------------------------')
        shell_cmd = f'rm -rf {self.sql_path}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('--------------------------删除表------------------------------')
        result = self.commonshpri.executDbSql(f'drop table if exists {self.tblname};')
        self.log.info(result)

        self.log.info("---------------------drop database--------------------")
        result = self.commonshpri.executDbSql(f'drop database {self.dbname};')
        self.log.info(result)

        self.log.info('----------Opengauss_Reliability_Tools_Restore_Case008.py 执行结束-----------------------')
